package com.leyou.item.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    public ResponseEntity<PageResult<BrandDTO>> pageQuery(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "desc", required = false) Boolean desc,
            @RequestParam(value = "key", required = false) String key
    ) {

        return ResponseEntity.ok(brandService.pageQuery(page, rows, sortBy, desc, key));
    }

    //品牌信息的保存逻辑
    @PostMapping
    public ResponseEntity<Void> addBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {

        this.brandService.addBrand(brandDTO, cids);

        //返回201
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //品牌信息的修改逻辑
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO, @RequestParam("cids") List<Long> cids) {

        this.brandService.updateBrand(brandDTO, cids);

        //返回201
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据分类id查询分类对应的所有的品牌信息
     * @param id
     * @return
     */
    @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> queryByCategoryId(@RequestParam("id")Long id){

        return ResponseEntity.ok(this.brandService.queryByCategoryId(id));
    }

    /**
     * 根据多个id查询品牌
     * @param ids
     * @return
     */
    @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(this.brandService.queryBrandByIds(ids));
    }

    @GetMapping
    public ResponseEntity<BrandDTO> queryBrandById(@RequestParam("id")Long id){
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }

}
